#include <QMessageBox>
#include "GlobPref.h"
#include "DataCell.h"
#include "DlgPickDateTime.h"
#include "scImpl.h"

#define FCT "DlgPickDateTime::DlgPickDateTime"
DlgPickDateTime::DlgPickDateTime(const QDateTime & preset, QWidget *parent) :
	QDialog(parent)
{
	ctrc(Trc::WORK,APP,FCT)<<"start"<<endl;
	ui.setupUi(this);
	ui.calendar->setSelectedDate(preset.date());
	connect(ui.calendar, SIGNAL(selectionChanged()), this, SLOT(resultChangedSl()));
	ui.time->setTime(preset.time());
	connect(ui.time, SIGNAL(timeChanged(const QTime &)), this, SLOT(resultChangedSl()));
	connect(ui.butToday, SIGNAL(clicked()), this, SLOT(butTodaySl()));
	connect(ui.butMidnight, SIGNAL(clicked()), this, SLOT(butMidnightSl()));
	connect(ui.butCurrentTime, SIGNAL(clicked()), this, SLOT(butCurrentTimeSl()));
	connect(ui.butLate, SIGNAL(clicked()), this, SLOT(butLateSl()));
	connect(ui.butFarBack, SIGNAL(clicked()), this, SLOT(butFarBackSl()));
	connect(ui.butNow, SIGNAL(clicked()), this, SLOT(butNowSl()));
	connect(ui.butFarFuture, SIGNAL(clicked()), this, SLOT(butFarFutureSl()));
	if(!(GlobPref::get()->getDebugData()))
		ui.debugText->setVisible(false);
	resultChangedSl();
}

#undef FCT
#define FCT "DlgPickDateTime::~DlgPickDateTime"
DlgPickDateTime::~DlgPickDateTime()
{
}

#undef FCT
#define FCT "DlgPickDateTime::getResult"
QDateTime DlgPickDateTime::getResult() const
{
	return QDateTime(ui.calendar->selectedDate(), ui.time->time());
}

#undef FCT
#define FCT "DlgPickDateTime::accept"
void DlgPickDateTime::accept()
{
	QDateTime res(getResult());
	if(res<GlobPref::timeMinQ)
	{
		ctrc(Trc::WORK,APP,FCT)<<"result "<<res<<" test "<<GlobPref::timeMinQ<<endl;
		QString text(tr("Date and time settings before "));
		text+=GlobPref::timeMinQ.toString(DATETIME_USER);
		text+=" (UTC time) will be reset to this date.";
		QMessageBox::warning(this, tr("Time & Date Validation"), text);
		butFarBackSl();
	}
	if(res>GlobPref::timeMaxQ)
	{
		ctrc(Trc::WORK,APP,FCT)<<"result "<<res<<" test "<<GlobPref::timeMaxQ<<endl;
		QString text(tr("Date and time settings after "));
		text+=GlobPref::timeMaxQ.toString(DATETIME_USER);
		text+=" (UTC time) will be reset to this date.";
		QMessageBox::warning(this, tr("Time & Date Validation"), text);
		butFarFutureSl();
	}
	QDialog::accept();
}

#undef FCT
#define FCT "DlgPickDateTime::butTodaySl"
void DlgPickDateTime::butTodaySl()
{
	ui.calendar->setSelectedDate(QDate::currentDate());
	resultChangedSl();
}

#undef FCT
#define FCT "DlgPickDateTime::butMidnightSl"
void DlgPickDateTime::butMidnightSl()
{
	ui.time->setTime(QTime(0,0,0));
	resultChangedSl();
}

#undef FCT
#define FCT "DlgPickDateTime::butCurrentTimeSl"
void DlgPickDateTime::butCurrentTimeSl()
{
	ui.time->setTime(QTime::currentTime());
	resultChangedSl();
}

#undef FCT
#define FCT "DlgPickDateTime::butLateSl"
void DlgPickDateTime::butLateSl()
{
	ui.time->setTime(QTime(23, 59, 59));
	resultChangedSl();
}

#undef FCT
#define FCT "DlgPickDateTime::butFarBackSl"
void DlgPickDateTime::butFarBackSl()
{
	QDateTime loc(GlobPref::timeMinQ.toLocalTime());
	ui.time->setTime(loc.time());
	ui.calendar->setSelectedDate(loc.date());
	resultChangedSl();
}

#undef FCT
#define FCT "DlgPickDateTime::butNowSl"
void DlgPickDateTime::butNowSl()
{
	butTodaySl();
	butCurrentTimeSl();
	resultChangedSl();
}

#undef FCT
#define FCT "DlgPickDateTime::butNowSl"
void DlgPickDateTime::butFarFutureSl()
{
	QDateTime loc(GlobPref::timeMaxQ.toLocalTime());
	ui.time->setTime(loc.time());
	ui.calendar->setSelectedDate(loc.date());
	resultChangedSl();
}

#undef FCT
#define FCT "DlgPickDateTime::resultChangedSl"
void DlgPickDateTime::resultChangedSl()
{
	if(!(GlobPref::get()->getDebugData()))
		return;
	QDateTime utc(getResult().toUTC());
	QString txt("UTC: time_t ");
	txt+=QString::number(utc.toTime_t())+" Q "+utc.toString();
	ui.debugText->setText(txt);
}
